//
//  ViewController.m
//  FlutterHybirdDemo
//
//  Created by 石茗伟 on 2019/1/7.
//  Copyright © 2019 聽風入髓. All rights reserved.
//

#import "ViewController.h"
@import Flutter;

@interface ViewController ()

@property (nonatomic, strong) FlutterEngine *engine;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    NSString *frameworkPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:@"FlutterSDK.framework"];
    NSString *assetsPath = [frameworkPath stringByAppendingPathComponent:@"flutter_assets"];
    NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkPath];
    FlutterDartProject *dart = [[FlutterDartProject alloc] initWithPrecompiledDartBundle:frameworkBundle];
    if (!self.engine) {
        FlutterEngine *engine = [[FlutterEngine alloc] initWithName:assetsPath.lastPathComponent project:dart];
        [engine runWithEntrypoint:nil];
        self.engine = engine;
    }
    
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:self.engine nibName:nil bundle:nil];
    FlutterMethodChannel *messageChannel = [FlutterMethodChannel methodChannelWithName:@"testChannel" binaryMessenger:flutterViewController];
    [messageChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        if ([call.method isEqualToString:@"testAction"]) {
            NSLog(@"hello world");
        }
    }];
    [self addChildViewController:flutterViewController];
    flutterViewController.view.frame = self.view.bounds;
    [self.view addSubview:flutterViewController.view];
}

@end
